package com.redoor.app.stake.workflow.service.impl;

import com.redoor.app.stake.common.utils.JsonUtils;
import com.redoor.app.stake.workflow.model.BaseWorkorder;
import com.redoor.app.stake.workflow.model.WorkFlowRedisConstants;
import com.redoor.app.stake.workflow.service.BaseWorkOrderRedisService;
import com.redoornetwork.framework.data.redis.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Set;

import static org.apache.ibatis.ognl.DynamicSubscript.all;

/**
 * Created by wangjunlei on 2018/3/30.
 */
@Service
public class BaseWorkOrderRedisServiceImpl implements BaseWorkOrderRedisService{

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void putWorkOrderToRedis(BaseWorkorder baseWorkorder) {
        if(null != baseWorkorder && !StringUtils.isEmpty(baseWorkorder.getUuid())){
            redisUtils.set(WorkFlowRedisConstants.REDIS_WORKORDER_PRE+":"+baseWorkorder.getUuid(),JsonUtils.toJson(baseWorkorder));
        }
    }

    @Override
    public void putRemindWorkOrderToRedis(BaseWorkorder baseWorkorder) {
        if(null != baseWorkorder && !StringUtils.isEmpty(baseWorkorder.getUuid())) {
            redisUtils.push(WorkFlowRedisConstants.REDIS_REMIND_WORKORDER_PRE + ":" + baseWorkorder.getReviewrorgcode(), JsonUtils.toJson(baseWorkorder));
        }
    }

    @Override
    public BaseWorkorder getRemindWorkOrderFromRedis(String orgno) {
        if(StringUtils.isEmpty(orgno)){
            return null;
        }
        Object workorder = redisUtils.pop(WorkFlowRedisConstants.REDIS_REMIND_WORKORDER_PRE + ":" + orgno);
        if(workorder != null){
            return JsonUtils.readValue(workorder.toString(),BaseWorkorder.class);
        }
        return null;
    }

    @Override
    public void deleteRemindWorkOrderFromRedis(String orgcode,String uuid) {
        redisUtils.hdel(WorkFlowRedisConstants.REDIS_REMIND_WORKORDER_PRE+":"+orgcode,uuid);
    }

    @Override
    public BaseWorkorder getWorkOrderFromRedis(String uuid) {
        if(redisUtils.exists(WorkFlowRedisConstants.REDIS_WORKORDER_PRE+":"+uuid)){
            return JsonUtils.readValue(redisUtils.get(WorkFlowRedisConstants.REDIS_WORKORDER_PRE+":"+uuid).toString(),BaseWorkorder.class);
        }
        return null;
    }

}
